package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/life", loadOnStartup = 1, initParams = {
        @WebInitParam(name = "username", value = "admin"),
        @WebInitParam(name = "password", value = "123456")
})
public class LifeServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        System.out.println("LifeServlet.init");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("LifeServlet.service");
        String username = getInitParameter("username");
        String password = getInitParameter("password");
        System.out.println("username:" + username + ",password:" + password);
    }

    @Override
    public void destroy() {
        System.out.println("LifeServlet.destroy");
    }

    // doXXX方法表示对应请求方式的请求会访问对应的doXxx方法
    // 例如:post请求可以访问doPost方法,get请求会访问doGet
    // 而service方法可以实现任意请求
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("LifeServlet.doPost");
//    }

//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("LifeServlet.doGet");
//    }
//
//    @Override
//    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPut(req, resp);
//    }
//
//    @Override
//    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doDelete(req, resp);
//    }
}
